package com.bos.editor;

import com.bos.dao.SekolahDAO;
import com.bos.domain.Sekolah;

import java.beans.PropertyEditorSupport;

/**
 * User: Deny Prasetyo, S.T.
 * http://www.jasoet.com
 */
public class SekolahEditor extends PropertyEditorSupport {

    private SekolahDAO sekolahDAO;

    public SekolahEditor(SekolahDAO sekolahDAO) {
        this.sekolahDAO = sekolahDAO;
    }

    @Override
    public void setAsText(String npsn) throws IllegalArgumentException {
        System.out.println("npsn nya adalah = " + npsn);
        try {
            Sekolah result = sekolahDAO.findByNpsn(npsn);

            if (result != null) {
                setValue(result);
            } else {
                throw new IllegalArgumentException("Sekolah with npsn " + npsn + " not Found!");
            }

        } catch (NumberFormatException ex) {
            throw new IllegalArgumentException(ex.getMessage());
        }
    }

    @Override
    public String getAsText() {
        if (getValue() != null) {
            Sekolah result = (Sekolah) getValue();
            return result.getNpsn() + "";
        } else {
            return "";
        }
    }
/*    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        try {
            long id = Long.parseLong(text);

            Sekolah result = sekolahDAO.get(id);

            if (result != null) {
                setValue(result);
            } else {
                throw new IllegalArgumentException("Sekolah with id " + id + " not Found!");
            }

        } catch (NumberFormatException ex) {
            throw new IllegalArgumentException(ex.getMessage());
        }
    }

    @Override
    public String getAsText() {
        if (getValue() != null) {
            Sekolah result = (Sekolah) getValue();
            return result.getId() + "";
        } else {
            return "";
        }
    }*/
}
